<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;


/**
 * TransferCurrency模型
 */
class TransferCurrency extends ModelBase
{

    protected $insert = ['create_time','serial_number'];

    /**
     * 流水号自动完成
     */
    public function setSerialNumberAttr($value)
    {

        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).rand(1000,9999);
    }

    /**
     * 连用户表
     */
    public function member(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 连钱包表
     */
    public function wallet(){
        return $this->belongsTo('Wallet','wallet_id','id');
    }










       /**
     * 获取状态信息
     */
    public function getStateTextAttr(){
        $arr=['待审核','已完成','失败','已审核'];
        return lang($arr[$this->state]);
    }




    /**
     * 获取类型信息
     */
    public function getTypeTextAttr(){
        $arr=['','币币账户到法币账户','法币账户到币币账户','卖出','买入'];
        return lang($arr[$this->data['type']]);
    }
    /**
     * 获取余额信息
     */
    public function getBeforeAvailableBalanceAttr($value){
        if($this->type==4){
            if($this->state==1){
                return $value;
            }else{
                return UserAccount::where(['user_id'=>$this->user_id,'wallet_id'=>$this->form_wallet_id])->value('balance');
            }
        }
        return $value;

    }

    /**
     * 获取到账余额信息
     */
    public function getAccountBalanceAttr(){
        switch ($this->data['type']){
            case  4 :
                $proportion1 = $this->walletRechargeType->proportion1;
                return sprintf("%.2f",$proportion1 * $this->number);
                break;
            case  2 :
                $proportion1 = $this->wallet->conversion_available;
                return sprintf("%.2f",$proportion1 * $this->number);
                break;
            case  9 :
                $from_price = $this->wallet->parities;
                $to_price = $this->wallet()->where(['id'=>$this->to_wallet_id])->value('parities');
                return sprintf("%.8f",($this->number*$from_price/$to_price));
                break;
            default:
                return 0.00;
                break;
        }

    }

    /**
     * 获取余额信息
     */
    public function getAfterAvailableBalanceAttr($value){
        if($this->type==4){
            if($this->state==1){
                return $value;
            }else{
                $proportion1=$this->walletRechargeType->proportion1;
                return sprintf("%.2f",UserAccount::where(['user_id'=>$this->user_id,'wallet_id'=>$this->form_wallet_id])->value('balance')+ $this->number*$proportion1);
            }
        }
        return $value;

    }

    /**
     * logo
     */
    public function getNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }

    /**
     * logo
     */
    public function getCcNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }




    /**
     * logo
     */
    public function getPoundageNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }


    /**
     * logo
     */
    public function getToNameAttr($value)
    {
        return  $this->wallet()->where(['id'=>$this->to_wallet_id])->value('name');
    }
    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }
}
